package com.yyjj.zixun.fragmnet;

import android.os.Bundle;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.mode.CacheMode;
import com.yyjj.zixun.R;
import com.yyjj.zixun.adapter.BTBNewsAdapter;
import com.yyjj.zixun.adapter.HomeZiXunAdapter;
import com.yyjj.zixun.api.Api;
import com.yyjj.zixun.entity.BTBNewsEntity;
import com.yyjj.zixun.entity.HomeZiXunEntity;

import java.util.ArrayList;

import tech.com.commoncore.app.FastManager;
import tech.com.commoncore.base.BaseRefreshLoadFragment;
import tech.com.commoncore.constant.ApiConstant;
import tech.com.commoncore.entity.BaseTResp;


public class HomeZiXunFragment extends BaseRefreshLoadFragment<HomeZiXunEntity> {

    public static HomeZiXunFragment newInstance() {
        Bundle args = new Bundle();
        HomeZiXunFragment fragment = new HomeZiXunFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getContentLayout() {
        return R.layout.zixun_fragment;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        loadData(0);
        mRecyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    public BaseQuickAdapter<HomeZiXunEntity, BaseViewHolder> getAdapter() {
        return new HomeZiXunAdapter();
    }

    @Override
    public void loadData(int page) {
        //https://d.wanjinig.cn/yapi/news/web8btcNewsList
        ViseHttp.GET(Api.BTCTRADE_LIST_URL)
                .baseUrl(ApiConstant.BASE_URL)
                .addParam("category", 0 + "")
                .addParam("page", 1 + page + "")
                .setHttpCache(true)
                .cacheMode(CacheMode.FIRST_REMOTE)
                .request(new ACallback<BaseTResp<ArrayList<HomeZiXunEntity>>>() {
                    @Override
                    public void onSuccess(BaseTResp<ArrayList<HomeZiXunEntity>> data) {
                        if (data.code == 1) {
                            if (data.data != null) {
                                if (data.data.size() >= 5) {
                                    FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(), data.data.subList(0, 5), null);
                                } else {
                                    FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(), data.data, null);
                                }
                            } else {
                                FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(), null, null);
                            }
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(), null, null);
                    }
                });
    }

    @Override
    public boolean isRefreshEnable() {
        return false;
    }

    @Override
    public boolean isLoadMoreEnable() {
        return false;
    }
}
